package com.magic.interview.collection;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author laoma
 * @create 2022-01-17 0:26
 */
public class RandomStringGenerator<T> implements Iterable<T> {

    private final List<T> list;

    public RandomStringGenerator(List<T> list) {
        this.list = list;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            @Override
            public boolean hasNext() {
                return true;
            }

            @Override
            public T next() {
                return list.get((int) (list.size() * Math.random()));
            }
        };
    }


    public static void main(String[] args) {
        var list = Arrays.asList("List", "Tree", "Array");
        var gen = new RandomStringGenerator<String>(list);

        // for (var s : gen) {
        //     System.out.println(s);
        // }

        var it = gen.iterator();
        for (int i = 0; i < 100; i++) {
            String next = it.next();
            System.out.println(next);
        }
    }
}
